/*
 * $Id: ItemByCraftSkillInfo.java 26 2013-05-20 06:00:59Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao.ui.info;

import java.util.ArrayList;
import java.util.List;

import ru.olympus.antonyhands.ao.UIHelper;
import ru.olympus.antonyhands.ao.data.AOData;
import ru.olympus.antonyhands.ao.data.entities.AOItem;
import ru.olympus.antonyhands.ao.data.util.ByCraftLevelComparator;
import ru.olympus.antonyhands.ao.data.util.EItemParentType;
import ru.olympus.antonyhands.ao.data.util.ItemByUniversalParentFilter;
import ru.olympus.antonyhands.ao.util.GoogleIterable;
import ru.olympus.antonyhands.ao.util.StringUtilities;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.VerticalLayout;

/**
 * @author AntonyHands
 * @since 16.05.2013
 */
public class ItemByCraftSkillInfo extends CustomComponent
{
    private static final long serialVersionUID = -5905189125332524855L;

    private VerticalLayout mainLayout;

    private ComboBox comboBoxCraftSkill;

    private ComboBox comboBoxItem;

    private AOItemInfo itemInfo;

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public ItemByCraftSkillInfo()
    {
        buildMainLayout();
        setCompositionRoot(mainLayout);
        // TODO add user code here
        UIHelper.refillSelect(comboBoxCraftSkill, AOData.getInstance().listCraftSkills());
    }

    private VerticalLayout buildMainLayout()
    {
        mainLayout = new VerticalLayout();

        comboBoxCraftSkill = new ComboBox();
        comboBoxCraftSkill.setCaption("Скилл");
        comboBoxCraftSkill.setTextInputAllowed(false);
        comboBoxCraftSkill.setImmediate(true);
        comboBoxCraftSkill.addListener(new ValueChangeListener()
        {
            private static final long serialVersionUID = -6993999711777392620L;

            @Override
            public void valueChange(ValueChangeEvent event)
            {
                Property prop = event.getProperty();
                refreshItemsList((String)prop.getValue());
            }
        });
        mainLayout.addComponent(comboBoxCraftSkill);

        comboBoxItem = new ComboBox();
        comboBoxItem.setCaption("Предмет");
        comboBoxItem.setTextInputAllowed(false);
        comboBoxItem.setNullSelectionAllowed(false);
        comboBoxItem.setImmediate(true);
        comboBoxItem.addListener(new ValueChangeListener()
        {
            private static final long serialVersionUID = -5022253936580780394L;

            @Override
            public void valueChange(ValueChangeEvent event)
            {
                String prop = (String)event.getProperty().getValue();
                if (!StringUtilities.isEmpty(prop))
                {
                    itemInfo.setAOItem(AOData.getInstance().getItemByCode(prop));
                }
            }
        });
        mainLayout.addComponent(comboBoxItem);

        itemInfo = new AOItemInfo();
        mainLayout.addComponent(itemInfo);

        return mainLayout;
    }

    private void refreshItemsList(String craftSkillCode)
    {
        List<AOItem> list = new ArrayList<AOItem>();
        if (!StringUtilities.isEmpty(craftSkillCode))
        {
            list = GoogleIterable.with(AOData.getInstance().listItems())
                    .filter(new ItemByUniversalParentFilter(EItemParentType.CRAFT_SKILL, craftSkillCode))
                    .sort(new ByCraftLevelComparator())
                    .toArrayList();
        }
        comboBoxItem.removeAllItems();
        for (AOItem item : list)
        {
        	comboBoxItem.addItem(item.getCode());
        	comboBoxItem.setItemCaption(item.getCode(),
        			StringUtilities.longToString(item.getCraftInfo().getCraftLevel()) + " - " +
        					item.getTitle());
        }
    }
}
